
<template>
    <div class="login-container">
        <div ref="particles" id="particles-js"></div>

        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
        >
            <div class="title-container">
                <h3 class="title">用户登录</h3>
            </div>

            <el-form-item prop="username">
                <span class="svg-container">
                    <i class="el-icon-user" />
                </span>
                <el-input
                    ref="userNameRef"
                    v-model="loginForm.username"
                    placeholder="请输入用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="on"
                />
            </el-form-item>

            <el-tooltip
                v-model="capsTooltip"
                content="Caps lock is On"
                placement="right"
                manual
            >
                <el-form-item prop="password">
                    <span class="svg-container">
                        <i class="el-icon-lock" />
                    </span>
                    <el-input
                        :key="passwordType"
                        ref="passwordRef"
                        v-model="loginForm.password"
                        :type="passwordType"
                        placeholder="请输入用户名"
                        name="password"
                        tabindex="2"
                        autocomplete="on"
                        @keyup="checkCapslock"
                        @blur="capsTooltip = false"
                        @keyup.enter="handleLogin"
                    />
                    <span class="show-pwd" @click="showPwd">
                        <svg-icon
                            :name="
                                passwordType === 'password'
                                    ? 'eye-off'
                                    : 'eye-on'
                            "
                        />
                    </span>
                </el-form-item>
            </el-tooltip>

            <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; height: 50px; font-size: 18px"
                @click.prevent="handleLogin"
            >
                登录
            </el-button>
        </el-form>
        <div class="starmask"></div>
    </div>
</template>



<script lang="ts">
import {
    defineComponent,
    ref,
    reactive,
    toRefs,
    onMounted,
    onUnmounted
} from 'vue'
import { useRouter, useRoute } from 'vue-router'
// import particlesjsConfig from './particlesjs-config.json'
import { useStore } from '@/store/index'
import { ActionTypesNamespaced } from '@/store/modules/user/action-types'

export default defineComponent({
    setup(props) {
        const userNameRef = ref(null)
        const passwordRef = ref(null)
        const loginFormRef = ref(null)
        const router = useRouter()
        const route = useRoute()
        const store = useStore()
        const state = reactive({
            loginForm: {
                username: 'admin',
                password: '111111'
            },
            loginRules: {
                username: [{ validator: userNameRef, trigger: 'blur' }],
                password: [{ validator: passwordRef, trigger: 'blur' }]
            },
            passwordType: 'password',
            loading: false,
            showDialog: false,
            capsTooltip: false,
            redirect: '',
            otherQuery: {}
        })
        const methods = reactive({
            validateUsername: () => {
                return
            },
            validatePassword: () => {
                return
            },
            checkCapslock: (e: any) => {
                const { key } = e
                state.capsTooltip =
                    key !== null && key.length === 1 && key >= 'A' && key <= 'Z'
            },
            showPwd: () => {
                // if (state.passwordType === 'password') {
                //     state.passwordType = ''
                // } else {
                //     state.passwordType = 'password'
                // }
                // nextTick(() => {
                //     ;(passwordRef.value as any).focus()
                // })
                return
            },
            handleLogin: async () => {
                // return
                state.loading = true
                const actionLogin: ActionTypesNamespaced =
                    'user/ACTION_SET_TOKEN'
                await store.dispatch(actionLogin)

                router
                    .push({
                        path: state.redirect || '/',
                        query: state.otherQuery
                    })
                    .catch((err) => {
                        console.warn(err)
                    })
                // Just to simulate the time of the request
                setTimeout(() => {
                    state.loading = false
                }, 0.5 * 1000)
            }
        })

        onMounted(() => {
            import(/* webpackChunkName: "jparticles" */ 'jparticles').then(
                (JParticles) => {
                    ;(window as any).effect = new JParticles.Particle(
                        '#particles-js',
                        {
                            color: 'rgba(255,255,255,0.2)',
                            lineShape: 'cube',
                            range: 2000,
                            proximity: 100,
                            // 开启视差效果
                            parallax: true,
                            lineWidth: 0.5,
                            parallaxLayer: [1, 2, 3]
                        }
                    )
                    // window.particlesJS('particles-js', particlesjsConfig)
                }
            )
        })
        onUnmounted(() => {
            ;(window as any).effect.pause()
        })
        return {
            userNameRef,
            passwordRef,
            loginFormRef,
            ...toRefs(state),
            ...toRefs(methods)
        }
    }
})
</script>

<style lang="scss">
.login-container .el-input {
    input {
        // color: $loginCursorColor;
        color: #fff;
    }
    input::first-line {
        color: #fff;
        // color: $lightGray;
    }
}

.login-container {
    position: fixed;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            height: 47px;
            background: transparent;
            border: 0px;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: #fff;
            // color: $lightGray;
            // caret-color: $loginCursorColor;
            -webkit-appearance: none;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $loginBg inset !important;
                -webkit-text-fill-color: #fff !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.5) !important;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 5px;
        // color: #454545;
    }
}
</style>

<style lang="scss" scoped>
.starmask {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    background: url(./dot_1.png) repeat center center;
}
#particles-js {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    // pointer-events: none;
    // background: #333;
    background: rgb(44, 46, 67);
}
.login-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
    // background-color: $loginBg;
    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 0px 35px 0px;
        margin: 0 auto 160px;
        overflow: hidden;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: #fff;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: #fff;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }

        .set-language {
            color: #fff;
            position: absolute;
            top: 3px;
            font-size: 18px;
            right: 0px;
            cursor: pointer;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $darkGray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}
</style>